/**
 * 
 */
package org.braketester.view;

import java.awt.BorderLayout;
import java.awt.ComponentOrientation;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.LinkedList;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import org.braketester.entry.AbstractApplyer;
import org.braketester.entry.ExceptionResolver;
import org.braketester.model.LTR212Preferences;
import org.braketester.model.LTRLoader;
import org.braketester.model.PreferencesLoader;

/**
 * @author LAP
 *
 */
public class OptionsFrame extends JFrame {
	private JTabbedPane content;
	private JPanel buttonsPanel;
	private JLTRPanel ltrPanel;
	private JButton apply;
	private JButton cancel;
	private LinkedList<AbstractApplyer<?>> applyers;
	
	private static final long serialVersionUID = 1L;

	public OptionsFrame() {
		super("Options");
		createGUI();
		loadPreferences();
		applyLoadedPreference();
	}
	
	private void applyLoadedPreference() {
		ltrPanel.getApplyer().apply();
	}

	public void addApplyer(AbstractApplyer<?> applyer) {
		applyers.add(applyer);
	}
	
	private void loadPreferences() {
		loadLTR212Preferences();
	}
	
	private void loadLTR212Preferences() {
		addApplyer(ltrPanel.getApplyer());
		LTR212Preferences preferences = tryLoadPreferences();
		if (preferences != null) ltrPanel.getApplyer().setPreferences(preferences); 
	}

	private LTR212Preferences tryLoadPreferences() {
		LTR212Preferences preferences = null;
		try {
			preferences = (LTR212Preferences) PreferencesLoader.getInstance().loadPreferences(LTR212Preferences.class);
		} catch (IOException e) {
			ExceptionResolver.getInstance().logException(e);
		}
		return preferences;
	}

	public JLTRPanel getLTRPanel() {
		return ltrPanel;
	}

	private void createGUI() {
		applyers = new LinkedList<AbstractApplyer<?>>();
		content = new JTabbedPane();
		ltrPanel = new JLTRPanel(LTRLoader.getInstance().getContainer());
		setLayout(new BorderLayout());
		add(content, BorderLayout.CENTER);
		createButtonsPanel();
		content.addTab("LTR", ltrPanel);
		pack();
		setResizable(false);
	}
	
	private void createButtonsPanel() {
		buttonsPanel = new JPanel();
		buttonsPanel.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
		buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.LINE_AXIS));
		apply = new JButton("Apply");
		apply.addActionListener(
				new OptionsFrameApplyButtonListener());
		cancel = new JButton("Cancel");
		cancel.addActionListener(
				new OptionsFrameCancelButtonListener());
		buttonsPanel.add(cancel);
		buttonsPanel.add(Box.createHorizontalStrut(5));
		buttonsPanel.add(apply);
		add(buttonsPanel, BorderLayout.PAGE_END);
	}
	
	private class OptionsFrameCancelButtonListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			for(AbstractApplyer<?> applyer : applyers) applyer.cancel();
			setVisible(false);
		}
	}
	
	private class OptionsFrameApplyButtonListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent event) {
			for(AbstractApplyer<?> applyer : applyers) applyer.apply();
			setVisible(false);
		}
	}
}
